package com.repair.datasource.handle;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

/**
 * @author chunyu
 * @date 2024/9/7,15:48
 * @apiNote
 */
public class DataObjectHandler implements MetaObjectHandler {

    /**
     * 在插入时 对以下字段设置默认值
     * 前两个是如果为null时添加当前时间
     * 后面两个就是 insert时就会设置为0
     * @param metaObject 元对象
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        this.setFieldValByName("deleted", 0, metaObject);
        this.setFieldValByName("version", 0, metaObject);
    }

    /** 这个方法就是修改时自动帮我们设置的值
     * updateTime 修改时间
     * @param metaObject 元对象
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
    }

}
